axios.defaults.baseURL='http://localhost:8081/'
Vue.prototype.$http=axios;

//解析url参数函数
function parseParamObj(url) {
    let paramsStr = url.substring(url.indexOf("?")+1); //typeId=27
    let paramsArray = paramsStr.split("&"); //typeId=27 xxx=xxx
    let paramObj = {};
    $.each(paramsArray,function(index,param){
        let parmName = param.split("=")[0];
        let parmValue = param.split("=")[1];
        paramObj[parmName] = parmValue;
    })
    return paramObj;
}

let excludeUrls=["/index.html","/callback.html","/binder.html"];
let url = location.href; //http://localhost:8081/about.html
let isRun=false;
if (url=="http://localhost:80/" || url=="wx.rongcarservice.com/"){
    isRun=true;
}
for (let i=0;i<excludeUrls.length;i++){
    if (url.indexOf(excludeUrls[i]) !=-1){
        isRun=true;
    }
}
if (!isRun){
    let logininfo = localStorage.getItem("loginInfo");
    if (!logininfo){
        alert("请登录再操作！")
    }
}

//1 使用axios前置拦截器，让所有的请求都携带token
axios.interceptors.request.use(config=>{
    //携带token
    let token =  localStorage.getItem("token");
    if(token){
        config.headers['token']=token;
    }
    return config;
},error => {
    Promise.reject(error);
})

//2 使用axios后置拦截器，处理没有登录请求
axios.interceptors.response.use(result=>{
    console.log(result.data+"jjjjjjj");
    let data = result.data;  // {success:false,message:"noLogin"}
    // 说明后端返回错误信息是 未登录，那么就要跳转到登录页面
    if(!data.success && data.message==="noLogin"){
       location.href="/index.html"
    }
    return result;
},error => {
    Promise.reject(error);
})

router.afterEach(transition => {
    NProgress.done();
});
